package cn.inswf.utils {
	/**
	 * @author li
	 * 存储开关
	 * 最多保存32个开关
	 * 有效值为0-31
	 * var bf:BitField=new BitField();
	 * bf.open(3);
	 * bf.open(31);
	 * trace(bf.has(3),bf.has(31));
	 * trace(bf.value);
	 */
	public class BitField {
		protected var _value : int = 0;

		public function BitField(value : uint = 0) {
			_value = value;
		}

		public function has(value : uint) : Boolean {
			var key : int = getKey(value);
			return (_value & key) == key;
		}

		public function open(value : uint) : void {
			_value |= getKey(value);
		}

		public function close(value : uint) : void {
			_value &= ~getKey(value);
		}

		public function clear() : void {
			_value = 0;
		}

		protected function getKey(index : uint) : int {
			index %= 32;
			return 1 << index;
		}

		public function toString() : String {
			return String(uint(_value));
		}

		public function get value() : int {
			return _value;
		}

		public function set value(v : int) : void {
			_value = v;
		}
	}
}
